package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnMysql {

    public static void main(String[] args) throws Exception {
        //1.加载驱动 需要将驱动的地址配置到项目的依赖中
        Class.forName("com.mysql.cj.jdbc.Driver");
        //以下为自动关闭资源的try语句
        //Connection、Statement、ResultSet 都继承了AutoCloseable接口，都可以使用try语句来关闭
        try(
            //2.使用DriverManage获取数据库连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/management_system?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true","root","root");
            //3.使用 Connection创建 statement
            Statement stmt = conn.createStatement();
            //4.执行sql语句
            ResultSet rs = stmt.executeQuery("select * from t_sys_menu");
            ){
            while (rs.next()){
                System.out.println(rs.getInt(1));
                System.out.println(rs.getString(2));
            }
        }
    }
}
